home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / tweak16b.zip / TWEAK.CPP < prev    next >
C/C++ Source or Header  |  1993-11-29  |  11KB  |  389 lines

  1. /*
  2.     TWEAK 1.6ß - Mold your own VGA modes
  3.  
  4.     by Robert Schmidt of Ztiff Zox Softwear, (C) Copyright 1992-93
  5.  
  6.     For documentation, see TWEAK.DOC.
  7.  
  8.  
  9.     History:
  10.  
  11.     0.9
  12.         First version available to the public.
  13.         Aw, what the heck, I'll admit this is the first version ever...
  14.         I know the usage of C++ classes may look a little stupid,
  15.             but it works.
  16.         Another stupid, *stupid* thing is the mixed use of conio and
  17.             iostream, but hey, it works too!
  18.         This version is not commented whatsoever.
  19.         Pretty lame interface - no help.
  20.         Test screens assume text start at segment 0xb800, and graphics
  21.         at 0xa000.
  22.     1.0
  23.         Changed from small to large memory model.  Not that I need it, it
  24.             just makes the code more readable.
  25.         Commented heavily, and beautified the code.
  26.         Nearly rewrote the program completely, by using another OO strategy.
  27.         Changed use of abort() to exit(1).
  28.         Text test pattern now fills entire text buffer from 0xb800:0x0000
  29.             to 0xb800:0xffff.
  30.         TWEAK now captures whatever screen mode is active at startup,
  31.             instead of defaulting to BIOS mode 3 (80x25).
  32.         The screen is reset to the startup BIOS mode at exit, too.
  33.         Added 'S' and 'L' as synonyms for F9 and F10.
  34.         Added Shift+TAB as a companion to TAB.
  35.         Editing screen now uses 80x50 instead of 80x25.
  36.         Added one text test screen which uses the 8x8 font.
  37.         Added online help, not context sensitive as promised.  Maybe later.
  38.         Edititing screen is cleared when a test is initiated, so that it
  39.             is easier to see that something happened if the wrong test
  40.             screen is selected.
  41.         Removed startup banner.
  42.         Removed use of cout and cin for screen/keyboard I/O, in favour of
  43.             the colors and windows provided by conio.h.
  44.         Instead, I use fstreams for *file* I/O... much more convenient than
  45.             FILEs.
  46.         Made the interface more cheerful by using lots of colors.
  47.     1.1
  48.         Nuked a serious bug in the text test pattern.  At the end, it
  49.             wrote characters far outside of the video buffer, overwriting
  50.             0x800 bytes at the start of the 0xC000 segment.
  51.             Most PCs have ROM in this area, so I didn't notice the
  52.             bug until I ran TWEAK on some 486s with a SCSI harddisk.
  53.             I think those map some RAM to that segment, in any case
  54.             TWEAK crashed whenever the text pattern was used on those
  55.             machines.
  56.         I Decided not to read back the registers when returning from the
  57.             test screen.  It was not very usable, and imposed some
  58.             problems when using different test screens.
  59.         Never released.
  60.     1.5
  61.         Added the AutoDetect testing pattern, which makes use of my new,
  62.             mode independant VGA graphics library to make the testing
  63.             much more interesting and informative.  Might add simple
  64.             menus to this later.
  65.         Fixed a bug in the VGA library for Chained 256-color modes, which
  66.             caused scrolling to go four times as far as intended.
  67.         Made the help window larger, more readable.
  68.         Added question of confirmation to quit when ESC is pressed.
  69.         Added lots more sample modes (from xlib06).
  70.     1.6
  71.         Added the mode heuristics seen in the autodetect test screen to
  72.             the editor screen.  This gives the user instant feedback on
  73.             his work.
  74.         Fixed the RegTable::out() method to correctly reset the sequencer.
  75.             Result: no flickering when testing modes.
  76.         The autodetect screen sets the VGA palettes to the BIOS default.
  77. */
  78.  
  79. #ifndef __LARGE__
  80. # ifndef __COMPACT__
  81. #  ifndef __HUGE__
  82. #   error A large data model is required!
  83. #  endif
  84. # endif
  85. #endif
  86.  
  87.  
  88. #include <stdio.h>
  89. #include <dos.h>
  90. #include <stdlib.h>
  91. #include <conio.h>
  92. #include <fstream.h>
  93. #include <ctype.h>
  94. #include <string.h>
  95.  
  96. #include "Screen.HPP"
  97. #include "RegEdit.HPP"
  98. #include "TestPat.HPP"
  99. #include "detect.hpp"
  100.  
  101.  
  102. void helpWindow()
  103.     {
  104.     tempBuffer swap(textScr);
  105.     swap.save();
  106.     window(5,5,editWidth-5,33);
  107.     textattr(HELP_COLOR);
  108.     clrscr();
  109.     window(6,6,editWidth-6,32);
  110.     gotoxy(1,1);
  111.     cprintf("TWEAK 1.6ß by Robert Schmidt - Ztiff Zox Softwear\n\r"
  112.             "Quick reference help screen\n\r"
  113.             "\n\r"
  114.             "Up/Down/Home/End: select register\n\r"
  115.             "       Backspace: enable/disable register\n\r"
  116.             "    2 hex digits: set register value\n\r"
  117.             "             -/+: decrease/increase register value\n\r"
  118.             "           F1-F8: toggle register bits 7-0\n\r"
  119.             "       F9 or 'S': save register set to file\n\r"
  120.             "      F10 or 'L': load set from file\n\r"
  121.             "             'M': select BIOS mode to work from\n\r"
  122.             "   TAB/Shift-TAB: select test screen\n\r"
  123.             "           ENTER: show test screen\n\r"
  124.             "             'H': show this help window\n\r"
  125.             "             ESC: Quit TWEAK immediately\n\r"
  126.             "\n\r"
  127.             "The mode heuristics shown are NOT perfect.  It is practically\n\r"
  128.             "impossible to foresee what your screen will show.  Use the\n\r"
  129.             "values given as guidance, or hints.\n\r"
  130.             "\n\r"
  131.             "When viewing the autodetect test screen, you may use the\n\r"
  132.             "arrows to scroll over the virtual screen.  Press ESC or ENTER\n\r"
  133.             "to leave.\n\r"
  134.             "\n\r"
  135.             "For more information, see the TWEAK.DOC file.\n\r"
  136.             "Now press the 'any' key to return to the editor.\n\r"
  137.             );
  138.     getch();
  139.     window(1,1,editWidth,editHeight);
  140.     swap.restore();
  141.     }
  142.  
  143.  
  144. void prompt(char *p = 0, int attr = PROMPT_COLOR)
  145.     {
  146.     textattr(7);
  147.     gotoxy(42,editHeight-1);
  148.     clreol();
  149.     textattr(attr);
  150.     if (p)
  151.         cprintf(p);
  152.     }
  153.  
  154.  
  155. int main(int argc, char **argv)
  156.     {
  157.     // Initialize the register table with descriptions and all.
  158.     RegisterEditor regEditor(ifstream("TWEAK.DAT"));
  159.     TestPatterns test;
  160.  
  161.     int parentBiosMode = getBiosMode();
  162.     preparePalettes();
  163.     // Set our default editing screen, and get its dimensions
  164.     setBiosMode(3);
  165.     textmode(C4350);
  166.     clrscr();
  167.     text_info *ti = new text_info;
  168.     gettextinfo(ti);
  169.     editMode = ti->currmode;
  170.     editHeight = ti->screenheight;
  171.     editWidth = ti->screenwidth;
  172.     editSize = editHeight * editWidth;
  173.     delete ti;
  174.  
  175.     tempBuffer swap(textScr);    // Establish the temporary screen buffer
  176.  
  177.     unsigned char quit = 0;            // Non-zero when a quit command is
  178.                                     //    initiated.
  179.     int key;                        // The last key pressed.
  180.  
  181.     // Build the editing screen:
  182.  
  183.     regEditor.printAllCon();
  184.     gotoxy(42, editHeight-20);
  185.     textattr(HELP_COLOR);
  186.     cprintf("(Press H if you need help)");
  187.  
  188.     ModeInfo minfo(regEditor);
  189.  
  190.     // Start the main keyboard polling loop:
  191.  
  192.     while (!quit)
  193.         {
  194.         test.tellTest();
  195.         regEditor.showBitMask();
  196.         minfo.show();
  197.         regEditor.updateSelect();
  198.  
  199.         int key = getch();
  200.         if (!key)
  201.             key = getch() << 8;
  202.         else
  203.             key = tolower(key);
  204.  
  205. keyentry:
  206.         switch (key)
  207.             {
  208.             case 0x4700:            //HOME
  209.                 regEditor.setSelect(0);
  210.                 break;
  211.             case 0x4800:            //UP
  212.                 --regEditor;
  213.                 break;
  214.             case 0x4F00:            //END
  215.                 regEditor.setSelect(regEditor.getMaxReg());
  216.                 break;
  217.             case 0x5000:            //DOWN
  218.                 ++regEditor;
  219.                 break;
  220.  
  221.             // Function keys - toggle single bit in selected reg.
  222.             case 0x3B00:            //F1
  223.             case 0x3C00:
  224.             case 0x3D00:
  225.             case 0x3E00:
  226.             case 0x3F00:
  227.             case 0x4000:
  228.             case 0x4100:
  229.             case 0x4200:            //F8
  230.                 **regEditor ^= (1<<7-((key>>8)-0x3B));
  231.                 break;
  232.  
  233.             case 0x4300:            //F9
  234.             case 0x4400:            //F10
  235.                 // Handle file operations (save/load):
  236.                 char fname[63];
  237.                 int save=(key==0x4300);
  238.  
  239.                 // Prompt for filename.
  240.                 if (save)
  241.                     prompt("Save file name: ");
  242.                 else
  243.                     prompt("Load file name: ");
  244.                 gets(fname);
  245.                 if (strlen(fname) == 0)
  246.                     {
  247.                     prompt("File operation canceled.");
  248.                     break;
  249.                     }
  250.  
  251.                 prompt(0, ERROR_COLOR);    // prepare for error
  252.                 if (save)
  253.                     {
  254.                     ofstream out(fname, ios::trunc|ios::binary|ios::out);
  255.                     if (out.bad())
  256.                         cprintf("Error creating '%s'!", fname);
  257.                     else
  258.                         {
  259.                         out << regEditor;
  260.                         if (out.bad())
  261.                             cprintf("Error writing '%s'!", fname);
  262.                         else
  263.                             {
  264.                             prompt(fname);
  265.                             cprintf(" written.");
  266.                             }
  267.                         }
  268.                     }
  269.                 else
  270.                     {
  271.                     ifstream in(fname, ios::binary|ios::in|ios::nocreate);
  272.                     if (in.bad())
  273.                         cprintf("Error opening '%s'!", fname);
  274.                     else
  275.                         {
  276.                         in >> regEditor;
  277.                         if (in.bad())
  278.                             cprintf("Error reading '%s'!", fname);
  279.                         else
  280.                             {